package com.goods.shop.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.time.Instant;

//@Component
//@Aspect
public class LogAspect {
    private Logger logger= LoggerFactory.getLogger(LogAspect.class);
    @Around(value="execution(* com.goods.shop.controller.*.*(..))")
    public Object round(ProceedingJoinPoint joinPoint){
        logger.info("round.开始执行 logAspect================");
        /*int code=0;
        if(code==0){
            logger.info("我要返回了。。。。。。");
            return 1;
        }*/
        Signature signature=joinPoint.getSignature();
        String targetName=signature.getDeclaringTypeName()+"-----"+signature.getName();
        logger.info("round.targetName=="+targetName);
//        Instant start=Instant.now();
//        Instant end=Instant.now();
//        System.out.println((end.toEpochMilli()-start.toEpochMilli()));
        Object[] args=joinPoint.getArgs();
        Object proceed=null;
        try {
            proceed=joinPoint.proceed(args);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        logger.info("round.结束执行 logAspect================{}",proceed.toString());
        return proceed;
    }

    @Around("@within(com.goods.shop.annotation.Log) && @target(com.goods.shop.annotation.Log2)")
    public Object round2(ProceedingJoinPoint joinPoint){
        logger.info("round2.开始执行 logAspect================");
        Signature signature=joinPoint.getSignature();
        String targetName=signature.getDeclaringTypeName()+"-----"+signature.getName();
        logger.info("round2.targetName=="+targetName);
//        Instant start=Instant.now();
//        Instant end=Instant.now();
//        logger.info(end.toEpochMilli()-start.toEpochMilli());
        Object[] args=joinPoint.getArgs();
        Object proceed=null;
        try {
            proceed=joinPoint.proceed(args);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        logger.info("round2.结束执行 logAspect================{}",proceed.toString());
        return proceed;
    }
}
